#ifndef _VM_H_
#define _VM_H_

#include "../../include/types.h"

// SV39: 页表项与页表类型
typedef uint64 pte_t;
typedef uint64* pagetable_t; // 512 项，每项 8 字节，占 4KiB

// 基本接口
pagetable_t create_pagetable(void);
int map_page(pagetable_t pt, uint64 va, uint64 pa, int perm);
void destroy_pagetable(pagetable_t pt);

// 辅助遍历
pte_t* walk_create(pagetable_t pt, uint64 va);
pte_t* walk_lookup(pagetable_t pt, uint64 va);

// 调试：打印页表
void dump_pagetable(pagetable_t pt, int level);

// 内核 VM 初始化/启用
void kvminit(void);
void kvminithart(void);

// 用户地址空间管理
uint64 uvmalloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz, int perm);
void uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free);
void uvmclear(pagetable_t pagetable, uint64 va);
void uvmfree(pagetable_t pagetable, uint64 sz);
uint64 vmfault(pagetable_t pagetable, uint64 va, int read);
uint64 uvmdealloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz);

// 导出当前内核页表（只读）
extern pagetable_t kernel_pagetable;

#endif


